<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String rootPath=application.getRealPath("/");
%>
<%--声明全局变量--%>
<%!
   String db_url="jdbc:mysql://localhost:3306/canteen?allowPublicKeyRetrieval=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
   String db_user="root";
   String db_pwd="12345";
   String db_Driver="com.mysql.cj.jdbc.Driver";
   int pageSize=3;
   int linkNumber=5;//分页显示数
 %>
 <%--获取分页列表 --%>
 <%!
 public  List<Integer> linkLumbers(int pageNo,int pages) {
		int avg = linkNumber / 2;
		int start = pageNo - avg;
		if (start <= 0) {
			start = 1;
		}
		int end = start + linkNumber - 1;
		if (end > pages) {
			end = pages;
		}
		
		if ((end - start) <linkNumber) {
			start = end - linkNumber;
			start=start<=0?1:start;
		}
		List<Integer> result = new ArrayList<Integer>();
		for (int i = start; i <= end; i++) {
			result.add(new Integer(i));
		}
		return result;
	}
 
 
  %>
<%--数据库访问公共方法 --%>
<%!
   //声明获得连接成员方法
   public Connection getConn()throws Exception{
	Connection conn=null;
	 try{
		//Step1：加载驱动
		 Class.forName(db_Driver);
	    //Step2:获得连接
		conn=DriverManager.getConnection(db_url, db_user, db_pwd);
		return conn;
	 }catch(Exception e){
		 e.printStackTrace();
		 throw e;
	 }
	
  }
  //声明关闭数据连接资源的方法
  
   public void close(ResultSet rs,Statement stmt,Connection conn)throws SQLException{
	  try{
		  if(rs!=null){
			  rs.close();
		  }
		  if(stmt!=null){
			  stmt.close();
		  }
		  if(conn!=null){
			  conn.close();
		  }
	  }catch(SQLException e){
		  e.printStackTrace();
		  throw e;
	  }
  }
   public void close(Statement stmt,Connection conn)throws SQLException{
		 close(null, stmt, conn);
	  }
   public void close(ResultSet rs)throws SQLException{
		 close(rs, null, null);
	  }
%>

